<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\goods\Goods as GoodsGoods;
use app\api\server\Goods as ServerGoods;
use app\common\model\goods\Category;
use app\common\model\goods\Review;
use app\common\model\user\Collect;
use app\common\model\UserGroup;

/**
 * 商品接口
 */
class Goods extends Api
{
    protected $noNeedLogin = ['category', 'goods_lists', 'info'];
    protected $noNeedRight = ['*'];

    /**
     * @Author LK
     * @date: 2023-11-10 
     * @ApiTitle (分类)
     * @ApiMethod (GET)
     * @ApiParams (name="class", type="string", required=true, sample="", description="1=百村联盟,2=创业专区")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function category()
    {
        $params = request()->param();
        extract($params);
        if (!isset($class) || !$class) {
            $this->error('参数错误');
        }
        $lists = Category::all(['type' => $class]);
        $this->success('', $lists);
    }


    /**
     * @Author LK
     * @date: 2023-11-10 
     * @ApiTitle (商品列表)
     * @ApiMethod (GET)
     * @ApiParams (name="type", type="integer", required=true, sample="", description="1=百村联盟,2=创业专区")
     * @ApiParams (name="meal", type="string", required=true, sample="", description="套餐:all=全部,0=否,1=是")
     * @ApiParams (name="hot", type="string", required=true, sample="", description="首页推荐:0=否,1=是")
     * @ApiParams (name="category_id", type="integer", required=true, sample="分类ID（不传为全部）", description="分类ID（不传为全部）")
     * @ApiParams (name="keywords", type="integer", required=true, sample="", description="搜索条件")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function goods_lists()
    {
        $params = request()->param();
        try {
            $lists = ServerGoods::goods_list($params);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('', $lists);
    }
    /**
     * @Author LK
     * @date: 2023-11-10 
     * @ApiTitle (商品详情)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function info()
    {
        $params = request()->param();
        extract($params);
        $data = GoodsGoods::get($id, ['spec', 'group']);
        if (!$data) {
            $this->error('商品不存在');
        }
        if ($this->auth->isLogin()) {
            foreach ($data['spec'] as $k => $v) {
                $v->vip_price = ServerGoods::vip_price($data, $v, 1);
            }

            // $data->is_collect = Collect::get(['goods_id' => $id, 'user_id' => $uid]) ? true : false;
        }
        //  else {
        // $data->is_collect =  false;
        // }
        $this->success('', $data);
    }
}
